<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="章节名称" prop="novelChapter">
        <el-input
          v-model="queryParams.novelChapter"
          placeholder="请输入章节名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小说ID" prop="novelId">
        <el-input
          v-model="queryParams.novelId"
          placeholder="请输入小说ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['novel:chapter:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['novel:chapter:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['novel:chapter:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="chapterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="章节ID" align="center" prop="id" />
      <el-table-column label="章节名称" align="center" prop="novelChapter" :show-overflow-tooltip="true" />
      <el-table-column label="小说ID" align="center" prop="novelId" />
      <el-table-column label="文件路径" align="center" prop="filePath" :show-overflow-tooltip="true" />
      <el-table-column label="涉及人物" align="center" prop="refRole" :show-overflow-tooltip="true" />
      <el-table-column label="章节概述" align="center" prop="interpretation" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['novel:chapter:edit']"
          >修改</el-button>
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['novel:chapter:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改章节对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="章节名称" prop="novelChapter">
          <el-input v-model="form.novelChapter" placeholder="请输入章节名称" />
        </el-form-item>
        <el-form-item label="小说ID" prop="novelId">
          <el-input v-model="form.novelId" placeholder="请输入小说ID" />
        </el-form-item>
      <el-form-item label="文件路径" prop="filePath">
        <el-input v-model="form.filePath" placeholder="请输入文件路径" />
        <div class="form-tip">支持相对路径或完整URL，如：/uploads/chapter1.docx</div>
      </el-form-item>
        <el-form-item label="涉及人物" prop="refRole">
          <el-input v-model="form.refRole" placeholder="请输入涉及人物，多个用逗号分隔" />
          <div class="form-tip">例如：张三,李四,王五</div>
        </el-form-item>
        <el-form-item label="章节概述" prop="interpretation">
          <el-input v-model="form.interpretation" type="textarea" placeholder="请输入章节概述" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted, getCurrentInstance } from 'vue'
import { listChapter, getChapter, delChapter, addChapter, updateChapter } from "@/api/novel/chapter";
import { parseTime } from "@/utils/ruoyi";

const { proxy } = getCurrentInstance();

const chapterList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    novelChapter: null,
    novelId: null,
  },
  rules: {
    novelChapter: [
      { required: true, message: "章节名称不能为空", trigger: "blur" }
    ],
    novelId: [
      { required: true, message: "小说ID不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询章节列表 */
function getList() {
  loading.value = true;
  listChapter(queryParams.value).then(response => {
    chapterList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    novelChapter: null,
    novelId: null,
    filePath: null,
    refRole: null,
    interpretation: null
  };
  proxy.resetForm("form");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加章节";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const chapterId = row.id || ids.value;
  getChapter(chapterId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改章节";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["form"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateChapter(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addChapter(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const chapterIds = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除章节编号为"' + chapterIds + '"的数据项？').then(function() {
    return delChapter(chapterIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

onMounted(() => {
  getList();
});
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
  line-height: 1.4;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 表格操作按钮样式 */
:deep(.el-table .cell) {
  padding: 0 8px;
}

:deep(.el-button--text) {
  padding: 4px 8px;
  margin: 0 2px;
}

/* 搜索表单样式 */
:deep(.el-form--inline .el-form-item) {
  margin-right: 16px;
}

/* 操作按钮区域样式 */
.mb8 {
  margin-bottom: 8px;
}
</style>


